package com.maimao.info.controller;

import com.maimao.api.mall.info.SearchHistoryApi;
import com.maimao.core.model.auth.AuthUser;
import com.maimao.core.model.resp.Result;
import com.maimao.info.controller.base.BaseController;
import com.maimao.info.service.SearchHistoryService;
import com.maimao.model.info.SearchHistory;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author MaoLin Wang
 * @date 2020/12/29 9:28 下午
 */
@RestController
public class SearchHistoryController extends BaseController implements SearchHistoryApi {

    @Autowired
    private SearchHistoryService searchHistoryService;

    @GetMapping("/all")
    @Override
    public Result<?> history() {
        AuthUser authUser = checkAndGetAuthUser();

        return Result.okData(searchHistoryService.history(authUser.getId()));
    }

    @Override
    @PostMapping("/add")
    public Result<?> addHistory(@RequestBody Map<String, String> data) {
        String key = data.get("key");
        if (key == null || StringUtils.isEmpty(key)) {
            return Result.ok();
        }
        AuthUser authUser = checkAndGetAuthUser();
        searchHistoryService.addHistory(key, authUser.getId());
        return Result.ok();
    }

    @Override
    @PostMapping("/delete")
    public Result<?> deleteHistory(@RequestBody Map<String, String> data) {
        String key = data.get("key");
        if (key == null || StringUtils.isEmpty(key)) {
            return Result.ok();
        }
        AuthUser authUser = checkAndGetAuthUser();
        searchHistoryService.deleteHistory(key, authUser.getId());
        return Result.ok();
    }
}
